package com.dhwl.hsh.merchant.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.dhwl.hsh.lib.bean.resp.OrderListResponse;
import com.dhwl.hsh.lib.utils.DateUtil;
import com.dhwl.hsh.lib.utils.SystemTool;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.presenter.OrderPresenter;
import com.dhwl.hsh.merchant.ui.activity.BaseActivity;
import com.dhwl.hsh.merchant.ui.activity.CommentOrderActivity;
import com.dhwl.hsh.merchant.ui.activity.OrderDetailActivity;
import com.dhwl.hsh.merchant.ui.dialog.AlertDialogFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单列表适配器
 *
 * @author Derek.Kidd
 * @time 2016/4/23 17:06
 */
public class OrderListAdapter extends BaseAdapter<OrderListResponse> {

    OrderPresenter orderPresenter;

    public OrderListAdapter(Context context, List<OrderListResponse> list, OrderPresenter orderPresenter) {
        super(context, list);
        this.orderPresenter = orderPresenter;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = inflate(R.layout.item_layout_order, null);
            viewHolder = new ViewHolder(convertView);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final OrderListResponse item = getList().get(position);
        //单次、长期
        if (item.getOrderType() == Constant.Order.ORDER_TYPE_CYCLE) {
            viewHolder.tvOrderType.setText(context.getString(R.string.cycle));
        } else if (item.getOrderType() == Constant.Order.ORDER_TYPE_CYCLE) {
            viewHolder.tvOrderType.setText(context.getString(R.string.once));
        }
        //司机名
        viewHolder.tvDriverName.setText(item.getOrderAcceptUser());
        //评分
        viewHolder.tvDriverRate.setText(String.format(context.getString(R.string.user_rate), item.getAccceptDriverScore()));
        //订单状态 todo
        viewHolder.tvOrderStatus.setText(item.getOrderState() + "");

        String addresses = item.getOrderDestination();

//        Address startAddress = addresses.get(0);
//        Address endAddress = addresses.get(1);
//
//        viewHolder.tvOrderStartAdd.setText(String.format(context.getString(R.string.order_start_shipping_add), startAddress.getPosiName()));
//        String str = String.format(context.getString(R.string.order_start_shipping_add), endAddress.getPosiName()) + (addresses.size() > 2 ? "(等" +
//                addresses.size() + "个配送地)" : "");

        viewHolder.tvOrderStartAdd.setText(String.format(context.getString(R.string.order_start_shipping_add), addresses));
        viewHolder.tvOrderEndAdd.setText(String.format(context.getString(R.string.order_start_shipping_add), addresses));

        viewHolder.tvOrderStartTime.setText(DateUtil.getDate(Long.valueOf(item.getCreateTime()), DateUtil.FORMAT_DATE_YYYYMMDD_T_HHMM));
        viewHolder.tvOrderTotalPrice.setText(String.format(context.getString(R.string.order_total_price), item.getCostMoney()));

        viewHolder.btnCancelOrder.setTag(item.getOrderId());
        viewHolder.btnCommentOrder.setTag(item.getOrderId());
        viewHolder.btnFixOrder.setTag(item);
        viewHolder.btnTelDriver.setTag(item.getAccceptDriverMobile());

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra(Constant.Order.KEY_ORDER_ID, item.getOrderId());
                context.startActivity(intent);
            }
        });


        viewHolder.btnCancelOrder.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onCancelOrderClick(v);
            }
        });

        viewHolder.btnCommentOrder.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onCommentClick(v);
            }
        });

        viewHolder.btnFixOrder.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onFixClick(v);
            }
        });

        viewHolder.btnTelDriver.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onCallClick(v);
            }
        });

        return convertView;
    }

    @OnClick(R.id.btn_comment_order)
    void onCommentClick(View view) {
        final String orderId = (String) view.getTag();
        Intent intent = new Intent();
        intent.setClass(context, CommentOrderActivity.class);
        intent.putExtra(Constant.Order.KEY_ORDER_ID, orderId);
        context.startActivity(intent);
    }

    @OnClick(R.id.btn_cancel_order)
    void onCancelOrderClick(View view) {

        final String orderId = (String) view.getTag();
        AlertDialogFragment alertDialogFragment = AlertDialogFragment.newInstance(context.getString(R.string.dialog_title_cancel_order), context.getString(R.string.alert_cancel_order));

        alertDialogFragment.setOnDialogSureClick
                (new AlertDialogFragment.OnSureClickListener() {
                    @Override
                    public void onDialogSureClick() {
                        //1:商户取消订单
                        orderPresenter.cancelOrder(1, orderId);
                    }
                });
        alertDialogFragment.show(((BaseActivity) context).getFragmentManager(), "order_cancel_dialog");
    }

    @OnClick(R.id.btn_fix_order)
    public void onFixClick(View view) {
        final OrderListResponse orderListResponse = (OrderListResponse) view.getTag();
        // TODO: 2016/5/16
//        Intent intent = new Intent(context,);
    }

    @OnClick(R.id.btn_tel_driver)
    public void onCallClick(View view) {
        String phoneNum = (String) view.getTag();
        SystemTool.call((BaseActivity) context, phoneNum);
    }


    class ViewHolder {
        @Bind(R.id.tv_order_type)
        TextView tvOrderType;
        @Bind(R.id.tv_driver_name)
        TextView tvDriverName;
        @Bind(R.id.tv_driver_rate)
        TextView tvDriverRate;
        @Bind(R.id.tv_order_status)
        TextView tvOrderStatus;
        @Bind(R.id.tv_order_start_add)
        TextView tvOrderStartAdd;
        @Bind(R.id.tv_order_end_add)
        TextView tvOrderEndAdd;
        @Bind(R.id.tv_order_start_time)
        TextView tvOrderStartTime;
        @Bind(R.id.tv_order_total_price)
        TextView tvOrderTotalPrice;
        @Bind(R.id.btn_for_pay)
        Button btnForPay;
        @Bind(R.id.btn_tel_driver)
        Button btnTelDriver;
        @Bind(R.id.btn_verify_order)
        Button btnVerifyOrder;
        @Bind(R.id.btn_comment_order)
        Button btnCommentOrder;
        @Bind(R.id.btn_fix_order)
        Button btnFixOrder;
        @Bind(R.id.btn_cancel_order)
        Button btnCancelOrder;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
